#!/usr/bin/env python

#
# fusiond script
#
# Copyright 2006-2008 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import socket
import logging

import pathhack
from fusion import FusionDaemon
from fusion.db.util import get_default_path

def main(**kwargs):
    daemon = FusionDaemon(**kwargs)

    # This was originally for OpenDHT, but keeping it might be wise.
    socket.setdefaulttimeout(300)

    daemon.loop()

if __name__ == '__main__':
    from optparse import OptionParser

    parser = OptionParser()

    path = get_default_path()
    parser.add_option('--db', dest='path', default=path,
                      help='location of database (default is %s)' % path)
    parser.add_option('--port_offset', type='int', default=0,
                      help='offset to be added to all port numbers')

    parser.add_option('--nocrawl', dest='crawl',
                      action='store_false', default=True,
                      help='disable crawling')
    parser.add_option('--noescrow', dest='escrow',
                      action='store_false', default=True,
                      help='disable escrow agent')
    parser.add_option('--nosync', dest='sync',
                      action='store_false', default=True,
                      help='disable joining the peer-to-peer network')
    parser.add_option('--nohttp', dest='http',
                      action='store_false', default=True,
                      help='disable the web interface')

    parser.add_option('--sync_port', type='int', default=35800,
                      help='which port to listen to for sync connections'
                           ' (default is 35800)')
    parser.add_option('--sync_host', default='',
                      help='which interface to listen to for sync connections'
                           ' (default is all interfaces)')

    parser.add_option('--http_port', type='int', default=8080,
                      help='which port to listen to for HTTP requests'
                           ' (default is 8080)')
    parser.add_option('--http_host', default='localhost',
                      help='which interface to listen to for HTTP requests'
                           ' (default is localhost)')

    parser.add_option('--debug', action='store_true', default=False,
                      help='log debugging messages too')

    parser.add_option('--log', dest='logfile',
                      help='where to send log messages')

    options, args = parser.parse_args()
    if args:
        parser.error('incorrect number of arguments')

    if options.debug:
        level = logging.DEBUG
    else:
        level = logging.INFO

    logging.basicConfig(level=level,
                        filename=options.logfile,
                        format='%(asctime)s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')

    main(path = options.path,
         sync = options.sync,
         crawl = options.crawl,
         escrow = options.escrow,
         http = options.http,
         sync_host = options.sync_host,
         sync_port = options.sync_port + options.port_offset,
         http_host = options.http_host,
         http_port = options.http_port + options.port_offset)
